package com.yscp.ysdemo.controller;

import com.alibaba.fastjson.JSON;
import com.yscp.ysdemo.constant.ResultEnum;
import com.yscp.ysdemo.dto.LikeDto;
import com.yscp.ysdemo.dto.ResponseResult;
import com.yscp.ysdemo.dto.UserDetail;
import com.yscp.ysdemo.entity.Collect;
import com.yscp.ysdemo.entity.Focus;
import com.yscp.ysdemo.entity.Like;
import com.yscp.ysdemo.exception.BusinessException;
import com.yscp.ysdemo.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 保存用户配置
     * @param configType 配置的类型
     * @param action 配置的内容
     * @return
     */
    @PostMapping("/savePersonalConfig")
    private ResponseResult savePersonalConfig(Integer configType, String action){
        if (configType == null || action == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR);
        }
        return userService.savePersonalConfig(action,configType);
    }


    @GetMapping("/getPersonalConfig")
    private ResponseResult getPersonalConfig(Integer configType, Integer userId) throws IOException {
        if (configType == null || userId == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR);
        }
        return userService.getPersonalConfig(userId,configType);
    }



    /**
     * 和注册一样，用户名不能重名，用户名和签名不能有敏感词汇
     * @param userDetail
     * @return
     */
    @PutMapping("/updateUserMsg")
    private ResponseResult updateUserMsg(@RequestBody UserDetail userDetail) {
        return userService.updateUserMsg(userDetail);
    }




    @PutMapping("/changeLike")
    //改变点赞关系,前台给实体类型，不局限于post的赞，ajax
    private ResponseResult changeLike(@RequestBody @Valid LikeDto likeDto){
       return userService.changeLike(likeDto);
    }


    @PutMapping("/changeFocus")
    //改变关注关系。ajax
    private ResponseResult changeFocus(@RequestBody Focus focus)  {
        if(focus.getUserId() == null || focus.getEntityId() == null || focus.getEntityType() == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR);
        }
        return userService.changeFocus(focus);
    }




    @PutMapping("/changeCollect")
    //改变收藏关系，ajax
    private ResponseResult changeCollect(@RequestBody Collect collect) {
        if (collect.getEntityId() == null || collect.getEntityType() == null || collect.getUserId() == null){
            throw new BusinessException(ResultEnum.PARAM_ERROR);
        }
        return userService.changeCollect(collect);
    }


}
